package zack.gateway.core.datasource.unpooled;

import zack.gateway.core.datasource.Connection;
import zack.gateway.core.datasource.DataSource;
import zack.gateway.core.datasource.DataSourceType;
import zack.gateway.core.datasource.connection.DubboConnection;
import zack.gateway.core.mapping.HttpStatement;
import zack.gateway.core.session.Configuration;
import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.ReferenceConfig;
import org.apache.dubbo.config.RegistryConfig;
import org.apache.dubbo.rpc.service.GenericService;

/**非池化的数据源
 * @author A1793
 */
public class UnpooledDataSource implements DataSource {

    private Configuration configuration;
    private HttpStatement httpStatement;
    private DataSourceType type;


    @Override
    public Connection getConnection() {
        switch (type){
            case HTTP://TODO
                ;break;
            case DUBBO:
            {
                //设置连接dubbo服务需要的信息
                ApplicationConfig applicationConfig = configuration.getApplicationConfig(httpStatement.getApplication());
                RegistryConfig registryConfig = configuration.getRegistryConfig(httpStatement.getApplication());
                ReferenceConfig<GenericService> referenceConfig = configuration.getReferenceConfig(httpStatement.getInterfaceName());
                return new DubboConnection(applicationConfig,registryConfig,referenceConfig);

            }
        }
        return null;
    }

    public void setConfiguration(Configuration configuration) {
        this.configuration = configuration;
    }

    public void setHttpStatement(HttpStatement httpStatement) {
        this.httpStatement = httpStatement;
    }

    public void setType(DataSourceType type) {
        this.type = type;
    }
}
